home *** CD-ROM | disk | FTP | other *** search
- Path: news.jhu.edu!news
- From: Harold Bien <hbien@bme.jhu.edu>
- Newsgroups: comp.object,comp.lang.c++,comp.ai.alife,sci.comp-aided
- Subject: [Q] Growing Objects
- Date: Thu, 18 Jan 1996 12:25:43 -0500
- Organization: Johns Hopkins University
- Message-ID: <30FE8297.41C6@bme.jhu.edu>
- NNTP-Posting-Host: 128.220.160.162
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b4 (X11; I; IRIX 5.3 IP22)
-
- First - thanks to all who responded to my earlier question. I
- found all the comments helpful and was (ashamedly) surprised at the
- number of responses.
-
- That said, I've decided to implement a "possible" solution which
- has not yet been discussed. Before I begin, let me preface this with
- the fact that I've invested quite a lot of time and effort into the
- simulation already and thus am not very willing to switching languages.
- Therefore, my current implementation is in C++ - my original language of
- choice. However, I did look into SmallTalk as per one suggestion and
- found that SmallTalk would have been better if it were not for the fact
- that it's an interpreted language (or at least the version we have).
- Anyway - this is all off topic.....
-
- In reality, each cell carries all the genetic codes which it
- requires with itself from the moment of "birth" (mitosis). Yes, the
- cell does control it genetic expression and hence its function, but the
- fact remains that ALL THE RAW MATERIALS existed in the cell - it's mere
- a function of selecting which ones to "turn on."
- Following this logic then, I decided that I wished to pack all the
- functionality of the immature/growing/mature cell into one large object.
- This gives me the advantage of recycling common code and even sharing
- common data.
- The problem now was selecting which function to run. Since this is
- not only being posted in comp.lang.c++, I will try and refrain from
- making the following description C++ specific, but if I do, please
- excuse me - I've had no formal training in OO parlance....
- My solution was to create an object "Cell." It has the common data
- members of cells in general (age, size, position, etc.), as well as the
- common member functions (Run, Grow, Move, etc.) where "Run" is the
- "function of life." Because computers are sequencial, I cannot run all
- my cells simultaneously. Therefore, I have to implement the function
- "Run" which will run my cells sequencially.
- This "Run" function will call another function indirectly. In
- simpler terms, it "opens up a box labelled RunMe" and whatever was in
- the box then gains control, and here's where the exciting part comes.
- The function which is "in the box" _CHANGES_OVER_TIME. Therefore, the
- functionality of the object "Cell" changes over time.
- Since this is all very abstract, I'm going to try to explain it
- using C++ (which I hope most of you are familiar with (I know, this is
- not comp.lang.c++, but very often C is chosen as an example language)...
- The class Cell has a function Run() which takes no arguments but
- calls another function through a pointer to a member function. Then,
- when that member function runs, it updates a variable 'age' and checks
- to see if it is mature. If so, then it will change the pointer to point
- to another function. Here's what it will look like:
-
- class Cell
- {
- public: // These fuctions are accessible to the public
- Cell(); /* This is the constructor - it gets called first when the
- object is created */
- // Run is the function which gets called from the 'outside'
- void Run();
- // PreMature is the function which runs when Run() is called and age<4
- void Run_PreMature();
- // PostMature is the function which is called when Run() is invoked
- // and age >=4
- void Run_PostMature();
-
- private:
- void (*Cell::RunFunc)(); /* Pointer to a cell function */
- unsigned int age;
- };
-
- Cell::Cell() /* Constructor */
- {
- age=0; // Newborn
- RunFunc=Run_PreMature; /* Point RunFunc to PreMature() function */
- }
-
- void Cell:Run_PreMature()
- {
- age=age+1; /* I know: (age++), but for those who don't know C... */
- if (age>=4) RunFunc=Run_PostMature; /* If age greater than or equal to
- 4
- then point RunFunc to
- PostMature()
- */
- }
-
-
- This approach reflects real cells in that all the code which it
- needs for functioning are contained within. It is only a matter of the
- selection of which code segment (or genetic segment) to use (or
- replicate).
-
- I'd certainly appreciate criticisms on this approach before I dig
- in. If anyone has had experience in doing the above or another similar
- method is superior to the above, I'd love to hear about it.
-
- Again, thanks to all who replied...
-
-
- Harold
-